﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.LogicalGroups.CUiLGRCreateGroupForm
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows.Forms;
using Tecnomatix.Engineering;
using Tecnomatix.Engineering.Ui;


namespace DnProcessSimulateCommands.LogicalGroups;

public class CUiLGRCreateGroupForm : TxForm
{
  private CApLGRGroupManager m_appManager;
  private IContainer components;
  private Label m_lblName;
  private Label m_lblScope;
  private TextBox m_txBoxName;
  private TxObjComboBoxCtrl m_txObjComboBoxScope;
  private Label m_lblGroupedObjects;
  private TxObjGridCtrl m_txObjGridGroupedObjects;
  private Button m_btnCancel;
  private Button m_btnOK;

  public CUiLGRCreateGroupForm()
  {
    this.InitializeComponent();
    this.m_appManager = new CApLGRGroupManager();
  }

  public virtual void OnInitTxForm()
  {
    base.OnInitTxForm();
    this.InitScopeControl();
    this.InitGroupedObjectsGrid();
  }

  private void InitGroupedObjectsGrid()
  {
    this.m_txObjGridGroupedObjects.SetValidator((ITxValidator) new CApGroupTypeValidator((ITxTypeFilter) CApLGRGroupManager.LogicalChildrenFilter, new ValidateGroupObject(this.IsGroupedObjectValid)));
    this.m_txObjGridGroupedObjects.SetPickProviders(new TxPickProvider[3]
    {
      null,
      (TxPickProvider) 1,
      (TxPickProvider) 4
    });
    this.m_txObjGridGroupedObjects.ListenToPick = true;
    this.CustomizeGroupedObjectsGrid();
  }

  private void CustomizeGroupedObjectsGrid()
  {
    string createGroupGridTitle = LGRLogicalGroupResource.CREATE_GROUP_GRID_TITLE;
    TxGridColumnData txGridColumnData = new TxGridColumnData();
    txGridColumnData.Init(createGroupGridTitle, (TxGridColumnType) 0);
    TxGridColumnDataCollection columnDataCollection = new TxGridColumnDataCollection();
    columnDataCollection.AddItem(txGridColumnData);
    TxGridDescription txGridDescription = new TxGridDescription();
    txGridDescription.SetData(columnDataCollection, 0);
    this.m_txObjGridGroupedObjects.Init(txGridDescription);
  }

  private void InitScopeControl()
  {
    this.m_txObjComboBoxScope.SetValidator((ITxValidator) new CApGroupTypeValidator((ITxTypeFilter) new TxTypeFilter(typeof (ITxLogicalGroupCreation)), new ValidateGroupObject(this.IsScopeObjectValid)));
    this.m_txObjComboBoxScope.AddItem(LGRLogicalGroupResource.LOGICAL_ROOT_GROUPS, (ITxObject) TxApplication.ActiveDocument.LogicalRoot);
  }

  private void SetScope(ITxObject scopeObj)
  {
    if (scopeObj == null)
    {
      scopeObj = (ITxObject) TxApplication.ActiveDocument.LogicalRoot;
      this.m_txObjComboBoxScope.SelectObject(scopeObj);
    }
    else
      this.m_txObjComboBoxScope.Object = scopeObj;
  }

  private void PopulateGroupedObjectsList(CApLGRGroupCreationData lgGroupCreationData)
  {
    foreach (ITxObject groupedObject in (Collection<ITxObject>) lgGroupCreationData.GroupedObjects)
    {
      if (this.IsGroupedObjectValid(groupedObject) && !((Collection<ITxObject>) this.m_txObjGridGroupedObjects.Objects).Contains(groupedObject))
        this.m_txObjGridGroupedObjects.AppendObject(groupedObject);
    }
  }

  private bool IsScopeObjectValid(ITxObject obj)
  {
    bool flag = true;
    if (((Collection<ITxObject>) this.m_txObjGridGroupedObjects.Objects).Contains(obj))
      flag = false;
    return flag;
  }

  private bool IsGroupedObjectValid(ITxObject obj)
  {
    bool flag = true;
    if (obj == this.m_txObjComboBoxScope.Object || CApLGRLogicalGroupsUtil.IsSubComponent(obj) || CApLGRLogicalGroupsUtil.IsPrototypeAssignment(obj) || CApLGRLogicalGroupsUtil.IsChildOrGripFrame(obj))
      flag = false;
    return flag;
  }

  private void EnableOKButton()
  {
    if (this.m_txObjComboBoxScope.Object != null && this.m_txBoxName.Text.Length > 0)
      this.m_btnOK.Enabled = true;
    else
      this.m_btnOK.Enabled = false;
  }

  private void CUiCLGLogicalGroupForm_Load(object sender, EventArgs e)
  {
    CApLGRGroupCreationData lgGroupCreationData = this.m_appManager.InitGroupCreationDataFromSelection();
    this.SetScope((ITxObject) lgGroupCreationData.Scope);
    this.PopulateGroupedObjectsList(lgGroupCreationData);
    this.m_txBoxName.Text = lgGroupCreationData.Name;
    this.EnableOKButton();
    this.m_txObjGridGroupedObjects.Focus();
  }

  private void m_txObjComboBoxScope_Picked(object sender, TxObjComboBoxCtrl_PickedEventArgs args)
  {
    this.EnableOKButton();
  }

  private void m_txObjComboBoxScope_SelectionChanged(object sender, EventArgs e)
  {
    this.m_txObjComboBoxScope.HideErrorWindow();
    this.EnableOKButton();
  }

  private void m_txObjComboBoxScope_TypeInvalid(object sender, EventArgs e)
  {
    this.m_btnOK.Enabled = false;
  }

  private void m_txObjComboBoxScope_TypeValid(object sender, EventArgs e) => this.EnableOKButton();

  private void m_btnOK_Click(object sender, EventArgs e)
  {
    this.m_appManager.CreateNewGroup(new CApLGRGroupCreationData(this.m_txBoxName.Text, this.m_txObjGridGroupedObjects.Objects, (ITxObject) (this.m_txObjComboBoxScope.Object as ITxLogicalGroupCreation)));
    ((Form) this).Close();
  }

  private void m_btnCancel_Click(object sender, EventArgs e) => ((Form) this).Close();

  private void m_txBoxName_TextChanged(object sender, EventArgs e) => this.EnableOKButton();

  protected virtual void Dispose(bool disposing)
  {
    if (disposing && this.components != null)
      this.components.Dispose();
    base.Dispose(disposing);
  }

  private void InitializeComponent()
  {
    ComponentResourceManager componentResourceManager = new ComponentResourceManager(typeof (CUiLGRCreateGroupForm));
    this.m_lblName = new Label();
    this.m_lblScope = new Label();
    this.m_txBoxName = new TextBox();
    this.m_txObjComboBoxScope = new TxObjComboBoxCtrl();
    this.m_lblGroupedObjects = new Label();
    this.m_txObjGridGroupedObjects = new TxObjGridCtrl();
    this.m_btnCancel = new Button();
    this.m_btnOK = new Button();
    ((Control) this).SuspendLayout();
    componentResourceManager.ApplyResources((object) this.m_lblName, "m_lblName");
    this.m_lblName.Name = "m_lblName";
    componentResourceManager.ApplyResources((object) this.m_lblScope, "m_lblScope");
    this.m_lblScope.Name = "m_lblScope";
    componentResourceManager.ApplyResources((object) this.m_txBoxName, "m_txBoxName");
    this.m_txBoxName.Name = "m_txBoxName";
    this.m_txBoxName.TextChanged += new EventHandler(this.m_txBoxName_TextChanged);
    this.m_txObjComboBoxScope.DropDownHeight = 106;
    this.m_txObjComboBoxScope.DropDownStyle = ComboBoxStyle.DropDown;
    this.m_txObjComboBoxScope.ListenToPick = true;
    componentResourceManager.ApplyResources((object) this.m_txObjComboBoxScope, "m_txObjComboBoxScope");
    ((Control) this.m_txObjComboBoxScope).Name = "m_txObjComboBoxScope";
    this.m_txObjComboBoxScope.Object = (ITxObject) null;
    this.m_txObjComboBoxScope.PickLevel = (TxPickLevel) 1;
    this.m_txObjComboBoxScope.ValidatorType = (TxValidatorType) 1;
    this.m_txObjComboBoxScope.SelectionChanged += new EventHandler(this.m_txObjComboBoxScope_SelectionChanged);
    this.m_txObjComboBoxScope.TypeInvalid += new EventHandler(this.m_txObjComboBoxScope_TypeInvalid);
    this.m_txObjComboBoxScope.TypeValid += new EventHandler(this.m_txObjComboBoxScope_TypeValid);
    this.m_txObjComboBoxScope.Picked += new TxObjComboBoxCtrl_PickedEventHandler(this.m_txObjComboBoxScope_Picked);
    componentResourceManager.ApplyResources((object) this.m_lblGroupedObjects, "m_lblGroupedObjects");
    this.m_lblGroupedObjects.Name = "m_lblGroupedObjects";
    ((UserControl) this.m_txObjGridGroupedObjects).BorderStyle = BorderStyle.FixedSingle;
    this.m_txObjGridGroupedObjects.ChangeGeneralSelection = false;
    this.m_txObjGridGroupedObjects.CurrentRow = -1;
    this.m_txObjGridGroupedObjects.EnableMultipleSelection = true;
    this.m_txObjGridGroupedObjects.EnableRecurringObjects = false;
    this.m_txObjGridGroupedObjects.ListenToPick = false;
    componentResourceManager.ApplyResources((object) this.m_txObjGridGroupedObjects, "m_txObjGridGroupedObjects");
    ((Control) this.m_txObjGridGroupedObjects).Name = "m_txObjGridGroupedObjects";
    this.m_txObjGridGroupedObjects.PickLevel = (TxPickLevel) 1;
    this.m_txObjGridGroupedObjects.ReadOnly = false;
    this.m_txObjGridGroupedObjects.ValidatorType = (TxValidatorType) 3;
    this.m_btnCancel.DialogResult = DialogResult.Cancel;
    componentResourceManager.ApplyResources((object) this.m_btnCancel, "m_btnCancel");
    this.m_btnCancel.Name = "m_btnCancel";
    this.m_btnCancel.UseVisualStyleBackColor = true;
    this.m_btnCancel.Click += new EventHandler(this.m_btnCancel_Click);
    componentResourceManager.ApplyResources((object) this.m_btnOK, "m_btnOK");
    this.m_btnOK.Name = "m_btnOK";
    this.m_btnOK.UseVisualStyleBackColor = true;
    this.m_btnOK.Click += new EventHandler(this.m_btnOK_Click);
    ((Form) this).AcceptButton = (IButtonControl) this.m_btnOK;
    componentResourceManager.ApplyResources((object) this, "$this");
    ((ContainerControl) this).AutoScaleMode = AutoScaleMode.Font;
    ((Form) this).CancelButton = (IButtonControl) this.m_btnCancel;
    ((Control) this).Controls.Add((Control) this.m_btnOK);
    ((Control) this).Controls.Add((Control) this.m_btnCancel);
    ((Control) this).Controls.Add((Control) this.m_txObjGridGroupedObjects);
    ((Control) this).Controls.Add((Control) this.m_lblGroupedObjects);
    ((Control) this).Controls.Add((Control) this.m_txObjComboBoxScope);
    ((Control) this).Controls.Add((Control) this.m_txBoxName);
    ((Control) this).Controls.Add((Control) this.m_lblScope);
    ((Control) this).Controls.Add((Control) this.m_lblName);
    ((Form) this).FormBorderStyle = FormBorderStyle.FixedDialog;
    ((Form) this).MaximizeBox = false;
    ((Form) this).MinimizeBox = false;
    ((Control) this).Name = nameof (CUiLGRCreateGroupForm);
    this.ShouldCloseOnDocumentUnloading = true;
    ((Form) this).ShowIcon = false;
    ((Form) this).Load += new EventHandler(this.CUiCLGLogicalGroupForm_Load);
    ((Control) this).ResumeLayout(false);
    ((Control) this).PerformLayout();
  }
}
